package ExerChapter15Collection_;
// 使用ArrayList完成对对象 Car (name, price) 的各种操作

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

// add: 添加单个元素
// remove: 删除指定元素
// contains: 查找元素是否存在
// size: 获取元素个数
// isEmpty: 判断是否为空
// clear: 清空
// addAll: 添加多个元素
// containsAll: 查找多个元素是否都存在
// removeAll: 删除多个元素
// 使用增强for和迭代器来遍历所有的car，需要重写 Car 的toString方法
public class h2 {
    public static void main(String[] args) {
        List<Car> list = new ArrayList<>();
        Car car0 = new Car("linken", 300000);
        list.add(car0);
        Car car1 = new Car("kaluola", 100000);
        list.add(car1);
        list.add(new Car("mazida", 150000));
        list.add(new Car("benchi", 350000));

        List<Car> list1 = new ArrayList<>();
        list1.add(car0);
        list1.add(car1);

        list.remove(2);
        System.out.println("判断car1是否存在：" + list.contains(car1));
        System.out.println("集合有多少个元素：" + list.size());
        System.out.println("判断是否为空:" + list.isEmpty());
        list.clear();
        list.addAll(list1);
        System.out.println("判断list1是否存在list里面：" + list.containsAll(list1));

        list.removeAll(list1);
        System.out.println(list.isEmpty());

        for (Car car : list1) {
            System.out.println(car);
        }

        Iterator<Car> it = list1.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());

        }

    }

}

class Car {
    String name;
    double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + ", price=" + price + "]";
    }

}
